package proj3;

import evolution_superclasses.Genetic_Operators;
import evolution_superclasses.Genotype;

public class Proj3_Genetic_Operators extends Genetic_Operators {
	private double _valueMutationRate = -1;

	public Proj3_Genetic_Operators(int size) {
		super(size);
	}

	public double get_valueMutationRate() {
		return _valueMutationRate;
	}

	public void set_valueMutationRate(double bitMutationRate) {
		_valueMutationRate = bitMutationRate;
	}

	@Override
	public void crossOver(Genotype gen1, Genotype gen2) {
		int crossOverPoint = _rand.nextInt(_genSize);
		Proj3_Genotype p3g1 = (Proj3_Genotype)gen1;
		Proj3_Genotype p3g2 = (Proj3_Genotype)gen2;

		String string1 = p3g1.get_binaryString();
		String string2 = string1.substring(crossOverPoint);
		string1 = string1.substring(0, crossOverPoint);
		
		String string3 = p3g2.get_binaryString();
		String string4 = string3.substring(crossOverPoint);
		string3 = string3.substring(0, crossOverPoint);

		p3g1.set_binaryString(string1+string4);
		p3g2.set_binaryString(string3+string2);
	}

	@Override
	public void mutation(Genotype gen) {
		if(_valueMutationRate < 0)_valueMutationRate = 0.05; //default
		Proj3_Genotype p3g = (Proj3_Genotype)gen;
		String s = "";
		for(char c: p3g.get_binaryString().toCharArray())
			s += _valueMutationRate > _rand.nextDouble()?(c=='0'?"1":"0"):c;
		p3g.set_binaryString(s);
	}

}
